//
//  WikiViewController.m
//  UI_A段项目_iDaily Watch
//
//  Created by 徐继垚 on 14-10-16.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "WikiViewController.h"
#import "WikiTableViewCell.h"
#import "WikiData.h"
#import "WikiModel.h"
#import "WikiPushViewController.h"
@interface WikiViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation WikiViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        WikiData * data = [[WikiData alloc]init];
      
        [data getDataFromBlock:^(NSMutableArray* dataSource) {
            self.dataSource = dataSource;
             [_tableView reloadData];
           
        }];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
    [self loadWeather];
    // Do any additional setup after loading the view.
}
-(void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_tableView];
    
    
    
    
    
    
    
}
- (void)loadWeather
{
    //101180301
    
    NSURL *URL =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101070201.html"];
    NSError *error;
    NSString *stringFromFileAtURL = [[NSString alloc]
                                     initWithContentsOfURL:URL
                                     encoding:NSUTF8StringEncoding
                                     error:&error];
    NSString *strTempL;
    NSString *strTempH;
    NSString *strWeather;
    NSString *fileName=@"晴.png";
    if(stringFromFileAtURL !=nil)
    {
        
     //   NSLog(stringFromFileAtURL);
        NSArray *strarray = [stringFromFileAtURL componentsSeparatedByString:@"\""];
        
        for(int i=0;i<strarray.count;i++)
        {
            //NSLog([strarray objectAtIndex:i]);
            NSString *str = [strarray objectAtIndex:i];
            if(YES == [str isEqualToString:@"temp1"])//最高温度
            {
                strTempH = [strarray objectAtIndex:i+2];
            }
            else if(YES == [str isEqualToString:@"temp2"])//最低温度
            {
                strTempL = [strarray objectAtIndex:i+2];
            }
            else if(YES == [str isEqualToString:@"weather"])//天气
            {
                strWeather = [strarray objectAtIndex:i+2];
            }
            
        }
        
        
        NSString *sweather = [[NSString alloc]initWithFormat:@"景区天气:%@\n%@~%@",strWeather,strTempL,strTempH];
        
        
        if(NSNotFound != [strWeather rangeOfString:@"晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@",@"晴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"多云"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"多云.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"小雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雷"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雾"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雾.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"中雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"中雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"小雪"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"小雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"小雨"].location)
        {
            fileName = [[NSString alloc]initWithFormat:@"%@",@"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雨加雪"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雨夹雪.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"中雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"雷阵雨"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"雷阵雨.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"大雨转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"大雨转晴.png"];
        }
        if(NSNotFound != [strWeather rangeOfString:@"阴转晴"].location)
        {
            fileName =[[NSString alloc]initWithFormat:@"%@", @"阴转晴.png"];
        }
        
        if(sweather !=nil)
            self.weathText.text = sweather;
    }
   // NSLog(fileName);
    self.weathImage.image = [UIImage imageNamed:fileName];
    
    
    _aView2 = [[UIView alloc]initWithFrame:CGRectMake(100, 220, 200, 200)];
    _aView2.backgroundColor = [UIColor clearColor];
    //_aView2.alpha = 0.1;
    [self.view addSubview:_aView2];
    _buttonContent1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _buttonContent1.backgroundColor = [UIColor clearColor];
    //_buttonContent1.alpha = 0.9;
    [_buttonContent1 setBackgroundImage:[UIImage imageNamed:fileName] forState:UIControlStateNormal];
    _buttonContent1.frame = CGRectMake(0, 0, 200, 150);
    [_aView2 addSubview:_buttonContent1];
    
    _lableweather1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 150, 120, 40)];
    _lableweather1.backgroundColor = [UIColor clearColor];
    _lableweather1.text = [NSString stringWithFormat:@"大连 %@",strWeather];
    _lableweather1.textColor = [UIColor cyanColor];
    [_aView2 addSubview:_lableweather1];
    
    _lableweather2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 190, 50, 30)];
    _lableweather2.backgroundColor = [UIColor clearColor];
    _lableweather2.text = [NSString stringWithFormat:@"高:%@",strTempH];
    _lableweather2.textColor = [UIColor redColor];
    [_lableweather2 setFont:[UIFont systemFontOfSize:12]];
    [_aView2 addSubview:_lableweather2];
    
    _lableweather3 = [[UILabel alloc]initWithFrame:CGRectMake(50, 190, 50, 30)];
    _lableweather3.backgroundColor = [UIColor clearColor];
    _lableweather3.text =[NSString stringWithFormat:@"低:%@", strTempL];
    [_lableweather3 setFont:[UIFont systemFontOfSize:12]];
    _lableweather3.textColor = [UIColor yellowColor];
    [_aView2 addSubview:_lableweather3];
    
    [fileName release];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _dataSource.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
    
    static NSString* indefault = @"wiki";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:indefault];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indefault];
    }
           UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(50, 0, 320, 70)];
        image.image = [UIImage imageNamed:@"wiki.png"];
        [self.tableView addSubview:image];
    
    return cell;
    }
    if (indexPath.section == 1 ) {
        
        static NSString* indefault = @"wiki123";
        WikiTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:indefault];
        if (!cell) {
            cell = [[WikiTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indefault];
        }
        cell.accessoryType = YES;
        cell.label.text = [_dataSource[indexPath.row]title];
        cell.label2.text = [_dataSource[indexPath.row]pubdate];
        return cell;
    }
    
    
    
    return 0;
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
        
        
        
        WikiPushViewController * vc = [[WikiPushViewController alloc]init];
        vc.dataSource = _dataSource;
        vc.index = indexPath.row;
        
        [self.navigationController pushViewController:vc animated:YES];
        
        
        
        
    }
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 70;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
